/**
 * 添加或修改主题信息
 * 
 * @param {}
 *            subjectId
 * @param {}
 *            title
 * @return {}
 */
function addOrUpdateSubject(subjectId, title) {
	var addOrUpdateSubjectForm = new Ext.form.FormPanel({
		id : 'addOrUpdateSubjectForm',
		frame : true,
		layout : "form",
		buttonAlign : 'center',
		monitorValid : true,
		bodyBorder : false,
		fileUpload : true,// 上传文件设置为true
		reader : new Ext.data.JsonReader({
					success : 'success',
					root : 'data'
				}, [{
							name : 'id'
						}, {
							name : 'subjectName'
						}, {
							name : 'subjectPic'
						}, {
							name : 'subjectOrder'
						}, {
							name : 'subjectStatus'
						}]),
		items : [{
					xtype : 'hidden',
					id : 'subjectForm_id',
					name : 'id'
				}, {
					xtype : 'textfield',
					id : 'subjectForm_subjectName',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>主题名称",
					allowBlank : false,
					name : "subjectName"
				}, {
					xtype : 'numberfield',
					id : 'subjectForm_subjectOrder',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>主题排序",
					allowBlank : false,
					allowNegative : false,
					enableKeyEvents : true,
					minValue : 1,
					name : "subjectOrder"
				}, {
					xtype : 'dataDictionaryCmb',
					id : 'subjectForm_statusCmb',
					anchor : '100%',
					fieldLabel : "<span style='color:#F00'>*</span>主题状态",
					editable : false,
					autoLoad : true,
					allowBlank : false,
					defaultFirstRow : 1,
					hiddenName : 'subjectStatus',
					dictionaryType : PmsUtil.subject_status
				}, {
					id : 'subjectForm_upload',
					fieldLabel : '导入图片',
					fieldLabel : "<span style='color:#F00'>*</span>导入图片",
					name : 'upload',
					xtype : 'fileuploadfield',
					buttonText : '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;浏览&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
					blankText : "请选择您要导入的图片",
					width : 300,
					border : false,
					listeners : {
						'fileselected' : function(field, value) {
							var uploadImage = Ext.getCmp("subjectForm_upload");
							var img_reg = /\.([jJ][pP][gG]){1}$|\.([jJ][pP][eE][gG]){1}$|\.([gG][iI][fF]){1}$|\.([pP][nN][gG]){1}$|\.([bB][mM][pP]){1}$/;
							// 得到选择的图片路径
							var url = 'file://' + uploadImage.getValue();
							if (img_reg.test(url)) {
								var imageShow_box = Ext
										.getCmp("subjectForm_image"); // 预览的图片框对象
								if (Ext.isIE) {
									var imageShow_box_dom = imageShow_box
											.getEl().dom;
									imageShow_box_dom.src = Ext.BLANK_IMAGE_URL;// 覆盖原来的图片
									imageShow_box_dom.filters
											.item("DXImageTransform.Microsoft.AlphaImageLoader").src = url;
								}// 支持FF
								else {
									var f = document
											.getElementById("subjectForm_upload-file").files[0];
									Ext.get('subjectForm_image').dom.src = window.URL
											.createObjectURL(f);
								}
							} else {
								Ext.MessageBox.show({
											title : "错误提示",
											msg : "必须为图片格式",
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							}

						}
					}
				}, {
					xtype : 'box',
					fieldLabel : "预览图片",
					baseCls : "x-plain",
					id : 'subjectForm_image',
					autoEl : {
						width : 300,
						height : 200,
						tag : 'input',
						type : 'image',
						src : __ctxPath + "/images/monmonde.png",
						style : 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);width:160px;height:160px;text-align:center;',
						complete : 'off',
						onclick : 'clickHandler()'
					}
				}],
		buttons : [{
					id : 'auctionsResaleCommit',
					text : '确定',
					formBind : true,
					handler : function() {
						addOrUpdatSubject();
					}
				}, {
					text : "取消",
					handler : function() {
						addOrupdateSubjectWindow.close();
					}
				}]
	});
	var addOrupdateSubjectWindow = new Ext.Window({
				id : 'addOrUpdateSubjectWindow',
				closeAction : 'close',
				title : title,
				closable : true,
				plain : true,
				modal : true,
				width : 450,
				draggable: false, // 不可拖动
				resizable:false,  // 不可调整
				height : 340,
				buttonAlign : 'center',
				autoDestroy : true,
				layout : 'fit',
				items : [addOrUpdateSubjectForm],
				listeners : {
					'show' : function() {
						selectSubjectUpdateById(subjectId);
					},
					'beforeshow' : function() {
						addOrupdateSubjectWindow.center();
					}
				}
			});
	return addOrupdateSubjectWindow;

};
/**
 * 主题突变点击事件
 */
function clickHandler() {
	var imageShow_box = Ext.getCmp("subjectForm_image");
	var subjectPicPath = imageShow_box.getEl().dom.src;
	window.open(subjectPicPath, '_blank');
}
/**
 * 查询主题信息
 * 
 * @param {}
 *            subjectId
 */
function selectSubjectUpdateById(subjectId) {
	if (subjectId != null && subjectId != "undefined") {
		Ext.getCmp("addOrUpdateSubjectForm").getForm().load({
			deferredRender : false,
			url : __ctxPath + "/subjectManager/selectSubjectUpdateById",
			params : {
				subjectId : subjectId
			},
			waitTitle : "请稍候",
			waitMsg : "正在载入数据...",
			root : "data",
			success : function(response, options) {
				var data = options.result.data;
				// 设置状态
				if (data.subjectStatus) {
					var subjectStatus = Ext.getCmp("subjectForm_statusCmb");
					subjectStatus.setValue(data.subjectStatus); // 隐藏的值
					subjectStatus.setRawValue(PmsUtil
							.SubjectStatus(data.subjectStatus));// 显示的值
				}
				// 设置图片
				if (data.subjectPic) {
					Ext.getCmp('subjectForm_image').getEl().dom.src = data.subjectPic;
				}
			},
			failure : function(f, g) {
				Ext.MessageBox.show({
							title : "操作信息",
							msg : "数据加载失败!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		});
	}
}
/**
 * 添加或修改主题信息
 */
function addOrUpdatSubject() {
	var subjectForm = Ext.getCmp("addOrUpdateSubjectForm");
	var subjectId = Ext.getCmp("subjectForm_id").getValue();
	var uploadImage = Ext.getCmp("subjectForm_upload").getValue();
	if (subjectId == '' && uploadImage == '') {
		Ext.Msg.show({
					title : '提示信息',
					msg : '主题图片不能为空!',
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.WARNING
				});
		return;
	}
	if (subjectForm.getForm().isValid()) {
		Ext.Msg.confirm("提示框", "确认要保存主题信息吗?", function(button) {
					if (button == "yes") {
						subjectForm.getForm().submit({
							url : __ctxPath
									+ '/subjectManager/addOrUpdateSubject',
							waitTitle : "请稍候",
							waitMsg : "正在提交表单，请稍候......",
							failure : function(response, options) {
								var jsonData = Ext.util.JSON
									.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							},
							success : function(response, options) {
								var jsonData = Ext.util.JSON
									.decode(options.response.responseText);
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								Ext.getCmp("addOrUpdateSubjectWindow").close();
								var gridPanel = Ext.getCmp('subjectManagerGrid');
								if (typeof(gridPanel) != 'undefined') {
									gridPanel.store.reload();
								}
							}
						});
					}
				});
	}
}